//
//  ST_BTPhoneNumberController.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/9.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import MBProgressHUD
import SwifterSwift
import SwiftyUserDefaults

class ST_BTPhoneNumberController: UIViewController {

//    MARK:- properties
    var bind_phone_number = false
    
    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        
        if !bind_phone_number{
            MobClick.beginLogPageView("InputPhoneNumber")
        }
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        if !bind_phone_number{
            MobClick.endLogPageView("InputPhoneNumber")
        }
        
    }
    
    lazy var phone_icon_view_start_frame:CGRect = {
        return CGRect.init(origin: CGPoint.init(x: ScreenWidth, y: 72-20+statusBarHeight), size: CGSize.init(width: 24, height: 24))
    }()
    
    lazy var phone_icon_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.image = ST_ImageUtils.imageName("register_icon_emoji电话")
        return control
    }()
    
    lazy var phone_num_sub_label:UILabel = {
        let control = UILabel.init()
        control.text = "我们需要你的电话号码以识别身份"
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
        return control
    }()
    
    

    lazy var phone_num_prefix_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.cornerRadius = 6
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        control.setTitleForAllStates("+86")
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
        control.setTitleColorForAllStates(Color.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.setImageForAllStates(ST_ImageUtils.imageName("register_icon_国旗")!)
        
        return control
    }()

    lazy var next_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)

        control.setImageForAllStates(ST_ImageUtils.imageName("register_bot_下一步")!)
        
        control.setImage(ST_ImageUtils.imageName("register_bot_下一步diss"), for: UIControl.State.disabled)
        
        control.addTarget(self, action: #selector(next_action), for: UIControl.Event.touchUpInside)
        
        control.isEnabled = false
        
        return control
    }()
    
    lazy var phone_num_label:UILabel = {
        let control = UILabel.init()
        control.text = "注册/登录"
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
        
        return control
    }()
  
    
    lazy var phone_num_prefix_label:UILabel = {
        let control = UILabel.init()
        control.text = "+86"
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        
        return control
    }()
    
    lazy var phone_num_text_filed:UITextField = {
        
        let control = UITextField.init()
        control.placeholder = "电话号码"
        control.setPlaceHolderTextColor(Color.init(hexString: "#999999")!)
        control.textColor = Color.init(hexString: "#333333")
        control.tintColor = Color.init(hexString: "#333333")
        control.textAlignment = .left
        control.keyboardType = UIKeyboardType.numberPad
//        control.backgroundColor = Color.random
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        
        control.delegate = self
        return control
        
    }()
    
    lazy var line1:UIView = {
       let control = UIView()
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        return control
    }()
    
    lazy var license_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setTitleForAllStates("登录即同意“用户协议”和“隐私政策”")
        control.setTitleColorForAllStates(Color.init(white: 1, alpha: 0.6))
        control.titleLabel?.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(license_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    
//    MARK: - Life cycle
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        
        UIView.zenly_animate(withDelay: 0.25, animations: {
            
            self.phone_icon_view.x = 30
            
            self.phone_num_sub_label.x = 60
            
        }) { (f) in
            
        }
        
        UIView.zenly_animate(withDelay: 0.45,animations: {
            
            self.phone_num_prefix_btn.x = 30
            
        }) { (f) in
        }
        
        UIView.zenly_animate(withDelay: 0.55,animations: {
            
            self.phone_num_text_filed.x = self.phone_num_prefix_btn.frame.maxX + 15
            
        }) { (f) in
            
            UIView.zenly_animate(animations: {
                
                self.next_btn.alpha = 1
                
            }) { (f) in
                
                
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = Color.white
        
        //RegisterStepManager.shared.step = 2
        
        view.addSubview(phone_icon_view)
        phone_icon_view.frame = self.phone_icon_view_start_frame
      
        view.addSubview(phone_num_sub_label)
        phone_num_sub_label.frame = CGRect.init(origin: CGPoint.init(x: ScreenWidth, y: phone_icon_view.center.y - self.phone_icon_view_start_frame.height/2.0), size: CGSize.init(width: 270, height: self.phone_icon_view_start_frame.height))
        
        view.addSubview(phone_num_prefix_btn)
        phone_num_prefix_btn.frame = CGRect.init(origin: CGPoint.init(x: ScreenWidth, y: phone_icon_view.frame.maxY+120), size: CGSize.init(width: 66, height: 30))
        
        view.addSubview(phone_num_text_filed)
        phone_num_text_filed.frame = CGRect.init(origin: CGPoint.init(x: ScreenWidth, y: phone_icon_view.frame.maxY+120), size: CGSize.init(width: ScreenWidth - 110 - 30, height: 30))
//
        self.phone_num_text_filed.becomeFirstResponder()
        
        view.addSubview(next_btn)
        
        next_btn.frame = CGRect.init(x: ScreenWidth - 45 - 40, y: phone_num_text_filed.frame.maxY + 36, width: 45, height: 45)
        next_btn.alpha = 0.1
        
        if !bind_phone_number{
            back_thread_execute(afterDelay: 1) {
                ST_RegisterMonitor.shared.logger.report(fromController: self)
            }
        }
        
        //TianRegisterStepManager.shared.addHelperBtn(root_view: view)
    }
    

    
    // MARK: - Event
    @objc func next_action(){
        
        self.next_btn.isUserInteractionEnabled = false
        
        main_thread_execute(afterDelay: 2) {
            self.next_btn.isUserInteractionEnabled = true
        }
        
        guard let str = self.phone_num_text_filed.text else {
            self.next_btn.isUserInteractionEnabled = true
            return
        }
        
        //RegisterStepManager.shared.account = str
        
            PlayFeedBack()
        
        let api = PH.sendCaptchaApi
        
        
        self.show_indeterminate_h_u_d()
        
            
            JVrequest(api, parameters: ["phone":str,"scene":"register"], success: { (json) in
                
                self.hidden_indeterminate_h_u_d()
                
                self.next_btn.isUserInteractionEnabled = true
                
                let vc = ST_BTCaptchaController()
                
                vc.phone_number = str
                
                vc.bind_phone_number = self.bind_phone_number
                
                self.navigationController?.pushViewController(vc)
                
            }) { (code, str) in
                
                self.next_btn.isUserInteractionEnabled = true
                
                print("登录接口错误\(str)")
                
                self.toast(text: str)
                
            }

     
        
    }
    
    @objc func license_action(){
        
        
        
    }
    
}



extension ST_BTPhoneNumberController:UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        
        let currentString: NSString = textField.text! as NSString
        
        let newString: NSString =
            currentString.replacingCharacters(in: range, with: string) as NSString
            
        self.next_btn.isEnabled = newString.length > 10
        
        return newString.length <= 11
    }
    
}
